Public Class formparent

    ' Tells if the application can be exited.
    Private m_bAllowClose As Boolean = False

    ' Scanned image path.
    Private m_szScannedImagePath As String = ""

    ' Initialize the form.
    Public Sub Initialize(ByVal szScannedImagePath As String)
        m_szScannedImagePath = szScannedImagePath
    End Sub

    ' Fires when the dialog is loading.
    Private Sub formparent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Replace the title of the dialog.
        Me.Text = Me.Text.Replace("<version>", formmain.g_szVersion)

    End Sub

    ' Fires when the form is closing.
    Private Sub formparent_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' Prevent closing if not allowed.
        If Not m_bAllowClose Then
            e.Cancel = True
        End If

    End Sub

    ' Fires when the form is shown first time.
    Private Sub formparent_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        ' Collect information here.
        Dim szSelectedVaultName As String = ""
        Dim oVault As MFilesAPI.Vault = Nothing

selectvault:

        ' Select the vault.
        Dim formSelectVault As formselectvault = New formselectvault
        formSelectVault.ShowDialog()
        szSelectedVaultName = formSelectVault.GetSelectedVault()

        ' Check if the vault is selected.
        If (szSelectedVaultName = "") Then

            ' No vault selected (cancel clicked), so exit.
            GoTo Endform

        Else

            ' Proceed to identification.
            GoTo identification

        End If
        
identification:

        ' Identification.
        Dim formIdentification As formidentification = New formidentification
        formIdentification.Initialize(szSelectedVaultName)
        formIdentification.ShowDialog()
        oVault = formIdentification.GetVault()

        ' Check that the vault is selected.
        If (IsNothing(oVault)) Then
            GoTo selectvault
        Else
            GoTo store
        End If

store:

        ' Store.
        Dim formStore As formstore = New formstore
        formStore.Initialize(oVault, m_szScannedImagePath)
        formStore.ShowDialog()

        ' Check if back button is clicked.
        If (formStore.IsBackButtonClicked() = True) Then

            ' Back button clicked.
            GoTo identification

        ElseIf (formStore.IsAttachButtonClicked() = True) Then

            ' Attach button clicked.
            GoTo attach

        Else

            ' New document created.
            GoTo endform

        End If

attach:

        ' Attach.
        Dim formAttach As formattach = New formattach
        formAttach.Initialize(oVault, m_szScannedImagePath)
        formAttach.ShowDialog()

        ' Check if back button is clicked.
        If (formAttach.IsBackButtonClicked() = True) Then

            ' Back button clicked.
            GoTo store

        Else

            ' Exit.
            GoTo endform

        End If

endform:

        Try

            ' Log out.
            If (IsNothing(oVault) = False) Then
                oVault.LogOutWithDialogs(Me.Handle)
            End If

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Close the form.
        m_bAllowClose = True
        Me.Close()

    End Sub

End Class